<meta content-type = 'utf-8'/>

<?
include "connectDB.php";

//$xmlDoc->load("042011 Theoretical Computer Science\posts.xml");

if ($handle = opendir('../dataset/')) {

    /* loop over the directory. */
	$i=0;
    while (false !== ($file = readdir($handle))) {
		$i++;
		if($i < 3) continue;
		$topic = $file;
		$doc = new DOMDocument();

		$doc->load( '../dataset/'.$topic.'\posts.xml' );
		//echo $doc->saveHTML();
		$Post[1]='Question';
		$Post[2]='Answer';
		$comments = $doc->getElementsByTagName( "posts" );
		foreach( $comments as $comment ){
			rows = $comment->getElementsByTagName("row");
			foreach ($rows as $row){
				//echo $Post[$row->getAttribute("PostTypeId")] .':';
				//if($row->getAttribute("PostTypeId")==2)
				//echo 'to'. $row->getAttribute("ParentId");
				//echo $row->getAttribute("Title").'<br>';
				//echo '<span style="color: green">'.$row->getAttribute("Body").'</span>';
				//echo '<hr>';

				$Id = $row->getAttribute("Id");
				$PostTypeId = $row->getAttribute("PostTypeId");
				
				if($PostTypeId == 2)
					$ParentID = $row->getAttribute("ParentId");
				else
					$ParentID = -1;

				if($PostTypeId == 1)
					$AcceptedAnswerId = $row->getAttribute("AcceptedAnswerId");
				else
					$AcceptedAnswerId = -1;
	
				$CreationDate = $row->getAttribute("CreationDate");
				$Body = mysql_escape_string($row->getAttribute("Body"));
				$OwnerUserId = $row->getAttribute("OwnerUserId");
				$Title = mysql_escape_string($row->getAttribute("Title"));
				$Tags = mysql_escape_string($row->getAttribute("Tags"));
 
				$query ="Insert into so_posts values('$Id','$PostTypeId','$ParentID','$AcceptedAnswerId','$CreationDate','$Body','$OwnerUserId','$Title','$Tags','$topic')";
				mysql_query($query) or die(mysql_error());
			}
			//print_r($product);
			//$names = $product->getElementsByTagName( "row" );
			//$name = $names->item(0)->nodeValue;
			//echo $name;
		}
    }
}
?>